package com.uam.common.config;

import com.uam.handler.AuthenticationEntryPointHandler;
import com.uam.handler.CustomAccessDeniedHandler;
import com.uam.handler.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @Author GoryLee
 * @Date 2022/12/5 20:24
 * @Version 1.0
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Autowired
    private AuthenticationEntryPointHandler authenticationEntryPointHandler;

    @Autowired
    private CustomAccessDeniedHandler customAccessDeniedHandler;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 配置放行路径，不用验证
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //关闭csrf功能
                .csrf().disable()
                //不通过session获取securityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 允许匿名访问，登录后不能访问
                .antMatchers("/user/login").anonymous()
//                .antMatchers("/route/index").anonymous()
//                .antMatchers("/task/palyback").anonymous()

                // 无论登录还是未登录都能够访问
//                .antMatchers("/user/addUser").permitAll()
                .antMatchers("/swagger-ui.html").anonymous()
                .antMatchers("/doc.html").anonymous()
                .antMatchers("/swagger-ui/**").anonymous()
                .antMatchers("/webjars/**").anonymous()
                .antMatchers("/v3/**").anonymous()
                .antMatchers("/swagger-resources/**").anonymous()
                .antMatchers("/webjars/springfox-swagger-ui").anonymous()
                .antMatchers("/webjars/springfox-swagger-ui/**").anonymous()
//                .antMatchers("/test/**").anonymous()
//                .antMatchers("/data/record/export").anonymous()
//                .antMatchers("/bc/info/**").anonymous()
                // .antMatchers("/route/**").anonymous()
                // 除了上面的路径，其他都需要鉴权访问
                .anyRequest().authenticated();

        // 添加过滤器
        http
                .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        //异常处理
        http
                .exceptionHandling()
                // 认证异常处理器
                .authenticationEntryPoint(authenticationEntryPointHandler)
                // 授权异常处理器
                .accessDeniedHandler(customAccessDeniedHandler);

        // 添加跨域处理
        http
                .cors();

    }

    /**
     * 注入AuthenticationManager到spring容器中，用于用户登录时调用方法验证
     *
     * @return
     * @throws Exception
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    //    public static void main(String[] args) {
//
//        //$2a$10$fl.1X4NRsNcUjaTpfzb8Ve9JZR6Rgr1tmQ1HmFA720ye1VQl6T6Qm   --1234
//        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
//        String encode = passwordEncoder.encode("1234");
//        System.out.println(encode);
//
//
//    }
}
